package database.dbObjects;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import main.Main;
import objects.File;

public interface IDBFile extends IDBObject {
	
	public class DBFile extends DBObject {
		
		/*
		 * methods are final for optimization of the code (by the compiler)
		 * it is not possible anymore to inherit it after this declaration 
		 */
		
		public static final int maxLengthPath = 200;
		
		
		public final void init() throws Exception {
			
			createTable();
			createTriggers();
		}
		
		private final void createTable() throws Exception {
			
			String query = "create table File( " +
						   "Pathname varchar(" + maxLengthPath + ") unique not null, " +
						   "MoreInformation varchar(" + maxLengthInfo + "), " +
						   "primary key (Pathname)" +
						       "on conflict abort);";
			
			executeCreation(query);
		}
		
		private final void createTriggers() throws Exception {
			
			String query;
			query = "create trigger update_file_path after update of Pathname on File " +
			   	    "begin " +
				    	"update RelCourseFile set File_Pathname = new.Pathname " +
				    		"where (File_Pathname = old.Pathname); " +
				    	"update RelTaskFile set File_Pathname = new.Pathname " +
				    		"where (File_Pathname = old.Pathname); " +
				    "end;";
			executeCreation(query);
			
			query = "create trigger delete_file_path before delete on File " +
					"begin " +
	    	   			"delete from RelCourseFile where (File_Pathname = old.Pathname); " +
	    	   			"delete from RelTaskFile where (File_Pathname = old.Pathname); " +
	    	   		"end;";
			executeCreation(query);
		}
		
		public final boolean add(String path, String moreInfo) throws Exception {
			
			checkConsistance(path, moreInfo);
			
			Vector<Object> v = new Vector<Object>(2);
			v.add(0, path);
			v.add(1, moreInfo);
			
			return super.add("File", v);
		}
		
		public final boolean delete(String path) throws Exception {
			
			checkPathname(path);
			
			return super.delete("File", "Pathname", path);
		}
		
		public final boolean updatePathname(String path, String newPath) throws Exception {
			
			checkPathname(path);
			checkPathname(newPath);
			
			return super.update("File", "Pathname", path, "Pathname", newPath);
		}
		
		public final boolean updateMoreInfo(String path, String newInfo) throws Exception {
			
			checkPathname(path);
			checkMoreInformation(newInfo);
			
			return super.update("File", "Pathname", path, "MoreInformation", newInfo);
		}
		
		public File getFile(String pathName) throws Exception {
			// Returns the requested object or null if nothing was found
			
			Statement stat = Main.connection.createStatement();
			ResultSet rs = stat.executeQuery("select * from File " +
											 "where (Pathname = '" + pathName + "');");
			if (rs.next()) {
				File f = new File(rs.getString("Pathname"),
						rs.getString("MoreInformation"));
				rs.close();
				return f;
			}
			rs.close();
			return null;
		}
		
		public Vector<File> getAllFiles() throws Exception {
			
			Statement stat = Main.connection.createStatement();
			ResultSet rs = stat.executeQuery("select * from File;");
			
			Vector<File> v = new Vector<File>();
			int counter = 0;
			while (rs.next()) {
				v.add(counter, new File(rs.getString("Pathname"), 
						rs.getString("MoreInformation")));
				counter++;
			}
			rs.close();
			return v;
		}
		
		private final void checkConsistance(String path, String moreInfo) throws Exception {
			
			checkPathname(path);
			checkMoreInformation(moreInfo);
		}
		
		public final void checkPathname(String path) throws Exception {
			
			if (path == null) { throw (new Exception("Pathname needed!")); }
			if (path.length() < 1) {
				throw (new Exception("Pathname must be at least 1 character long!"));
			}
			if (path.length() > maxLengthPath) {
				throw (new Exception("Pathname exceeds maximum length (" +
					maxLengthPath + " characters)!"));
			}
			java.io.File f = new java.io.File(path);
			if (!f.exists()) {
				throw (new Exception("The file does not exist or the " +
						"given pathname is incorrect!"));
			}
		}
		
		private final void checkMoreInformation(String moreInfo) throws Exception {
			
			if (moreInfo == null) { moreInfo = "Nothing."; }
			if (moreInfo.length() > maxLengthInfo) {
				throw (new Exception("MoreInformation exceeds maximum length (" +
						maxLengthInfo + " characters)!"));
			}
		}
		
		
		private final int maxLengthInfo = 200;
		
	}
	
}
